package com.zbkj.crmeb.supplier.controller;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
        import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonResult;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.PageInfo;
import com.zbkj.crmeb.front.request.ProductRequest;
import com.zbkj.crmeb.front.response.ProductResponse;
import com.zbkj.crmeb.merchant.admin.controller.AdminBaseController;
import com.zbkj.crmeb.store.request.SupplierOpenShopRequest;
import com.zbkj.crmeb.store.request.SupplierStoreProductSearchRequest;
import com.zbkj.crmeb.store.response.StoreProductResponse;
import com.zbkj.crmeb.store.response.SupplierOpenShopResponse;
import com.zbkj.crmeb.supplier.model.SupplierOpenShop;
import com.zbkj.crmeb.supplier.service.SupplierOpenShopService;



/**
 * 供应商一键开店表
 *
 * @author lss
 * @email didiok@foxmail.com
 * @date 2024-11-18 17:36:49
 */
@Slf4j
@RestController
@RequestMapping("api/admin/supplier/open/shop")
public class SupplierOpenShopController extends AdminBaseController {
    @Autowired
    private SupplierOpenShopService supplierOpenShopService;

    /**
     * 分页显示店铺
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     */
    @ApiOperation(value = "店铺分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<SupplierOpenShopResponse>> getList(@Validated SupplierOpenShopRequest request, @Validated PageParamRequest pageParamRequest, HttpServletRequest servletRequest) {
        PageInfo<SupplierOpenShopResponse> pageInfo = new PageInfo<>();
        try {
            pageInfo = supplierOpenShopService.getList(request, pageParamRequest, getMerId(servletRequest));
        }catch (Exception e){
            log.error("店铺分页列表查询报错，", e);
            throw new CrmebException("店铺分页列表查询报错");
        }
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * 分页显示店铺(总后台使用)
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     */
    @ApiOperation(value = "店铺分页列表")
    @RequestMapping(value = "/listV2", method = RequestMethod.GET)
    public CommonResult<CommonPage<SupplierOpenShopResponse>> getListV2(@Validated SupplierOpenShopRequest request, @Validated PageParamRequest pageParamRequest, HttpServletRequest servletRequest) {
        PageInfo<SupplierOpenShopResponse> pageInfo = new PageInfo<>();
        try {
            pageInfo = supplierOpenShopService.getListV2(request, pageParamRequest);
        }catch (Exception e){
            log.error("店铺分页列表查询报错，", e);
            throw new CrmebException("店铺分页列表查询报错");
        }
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * 查询注册申请信息
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "详情")
    @GetMapping("/info/{id}")
    public CommonResult<SupplierOpenShop> info(@PathVariable Integer id) {
        SupplierOpenShop supplierOpenShop = new SupplierOpenShop();
        try {
            supplierOpenShop = supplierOpenShopService.getById(id);
        }catch (Exception e){
            log.error("店铺详情信息查询报错，", e);
            throw new CrmebException("店铺详情信息查询报错");
        }
        return CommonResult.success(supplierOpenShop);
    }

    /**
     * 详情
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "详情")
    @GetMapping("/detail/{id}")
    public CommonResult<SupplierOpenShopResponse> detail(@PathVariable Integer id, HttpServletRequest servletRequest) {
        SupplierOpenShopResponse shopResponse = new SupplierOpenShopResponse();
        try{
            shopResponse = supplierOpenShopService.shopDetail(id,getMerId(servletRequest));
        }catch (Exception e){
            log.error("店铺详情查询报错，", e);
            throw new CrmebException("店铺详情查询报错");
        }
        return CommonResult.success(shopResponse);
    }

    @ApiOperation(value = "一键开店商品分页列表")
    @GetMapping(value = "/listProduct")
    public CommonResult<CommonPage<StoreProductResponse>> listProduct(SupplierStoreProductSearchRequest request, HttpServletRequest servletRequest) {
        PageInfo<StoreProductResponse> pageInfo = new PageInfo<>();
        try {
            pageInfo = supplierOpenShopService.supplierProductList(request, getMerId(servletRequest));
        }catch (Exception e){
            log.error("一键开店商品分页列表错误，", e);
            throw new CrmebException("一键开店商品分页列表报错");
        }
        if (pageInfo == null) {
            return CommonResult.success(CommonPage.restPage(new ArrayList<>()));
        }

        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * 新增
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "新增")
    @PostMapping("/createOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> createOrUpdate(@RequestBody SupplierOpenShopRequest supplierOpenShop, HttpServletRequest servletRequest) {
        Boolean result = null;
        try {
            result = supplierOpenShopService.createOrUpdate(supplierOpenShop,getMerId(servletRequest));
        }catch (Exception e){
            log.error("一键开店新增错误，", e);
            throw new CrmebException("一键开店新增报错");
        }
        return CommonResult.success(result);
    }

    /**
     * 修改店铺
     * @param supplierOpenShop 修改参数
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "修改")
    @PutMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> update(@RequestBody @Validated SupplierOpenShop supplierOpenShop) {
        boolean update = false;
        try {
            int count = supplierOpenShopService.count(Wrappers.<SupplierOpenShop>lambdaQuery()
                    .eq(SupplierOpenShop::getTitle, supplierOpenShop.getTitle())
                    .eq(SupplierOpenShop::getSupplierId, supplierOpenShop.getSupplierId())
                    .ne(SupplierOpenShop::getId, supplierOpenShop.getId())
            );
            if (count > 0) {
                return CommonResult.failed("当前店铺名称已存在");
            }

            update = supplierOpenShopService.updateById(supplierOpenShop);
        }catch (Exception e){
            log.error("修改店铺错误，", e);
            throw new CrmebException("修改店铺报错");
        }
        return update ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 修改二维码
     * @param supplierOpenShop 修改参数
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "修改")
    @PutMapping("/image/update")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> updateImage(@RequestBody SupplierOpenShop supplierOpenShop) {
        boolean update = false;
        if(supplierOpenShop.getId() == null || supplierOpenShop.getId() == 0
                || StringUtils.isEmpty(supplierOpenShop.getPublicityImage())){
            throw new CrmebException("参数错误");
        }
        try {
            update = supplierOpenShopService.updateImgById(supplierOpenShop);
        }catch (Exception e){
            log.error("修改二维码报错，", e);
            throw new CrmebException("修改二维码报错");
        }
        return update ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public CommonResult<String> delete(@PathVariable Integer id) {
        boolean update = false;
        try {
            update = supplierOpenShopService.deleteShopById(id);
        }catch (Exception e){
            log.error("删除店铺报错，", e);
            throw new CrmebException("删除店铺报错");
        }
        return update ? CommonResult.success() : CommonResult.failed();
    }

}
